﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pharmacy.Data.Entity;
using Pharmacy.Data;
using System.Transactions;
using Pharmacy.Business;
using Pharmacy.Data.Core;
using Pharmacy.Ultilities;

namespace Pharmacy.UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestAddNew()
        {
            using(TransactionScope scope = new TransactionScope())
            {

                IObjectContext pharmacyContext = ObjectContextFactory.CreatePharmacyContext(UserName: "TestPharmacy", connectionString: Pharmacy.Ultilities.Configurations.ConfigManager.DIOConfig.ConnectionString);

                User u = new User();
                u.UserName = "tam";
                u.Password = CryptHelper.Encrypt("sa123456");
                u.IsActived = true;

                Repository<User> lstUser = new Repository<User>(pharmacyContext);
                lstUser.Add(u);
                pharmacyContext.SaveChanges();

                scope.Complete();
            }
        }

        //[TestMethod]
        public void TestLock()
        {
            using (TransactionScope scope = new TransactionScope())
            {

                try
                {
                    IObjectContext pharmacyContext = ObjectContextFactory.CreatePharmacyContext(UserName: "TestPharmacy", connectionString: @"data source=TAMNGUYEN-PC\MSSQLSERVER2008;initial catalog=Pharmacy;Persist Security Info=True;User ID=sa;Password=sa123456;multipleactiveresultsets=True;App=EntityFramework");
                    Repository<User> lstUser = new Repository<User>(pharmacyContext);
                    Repository<Function> lstFunction = new Repository<Function>(pharmacyContext);
                    PessimisticLocking pLock = new PessimisticLocking(pharmacyContext);

                    User u = lstUser.FirstOrDefault(tmpUser => tmpUser.ID == 1);
                    pLock.Lock(u);
                    u.FullName = "test";

                    Function f = lstFunction.FirstOrDefault(tmpFunction => tmpFunction.ID == 438);                    
                    f.Note = "asdads";
                    

                    pharmacyContext.SaveChanges();

                    scope.Complete();

                    //IObjectContext pharmacyContext1 = ObjectContextFactory.CreatePharmacyContext(UserName: "TestPharmacy1", connectionString: @"data source=DICTAMNGUYEN\SQLEXPRESS2008;initial catalog=Pharmacy;Persist Security Info=True;User ID=sa;Password=sa123456;multipleactiveresultsets=True;App=EntityFramework");
                    //Repository<User> lstUser1 = new Repository<User>(pharmacyContext1);
                    //User u1 = lstUser1.FirstOrDefault(user => user.ID == 2);

                }
                catch (PessimisticLockingException ex)
                { 
                    
                }
               
            }
        }
    }
}
